package com.swagger.wen.controller;

import com.swagger.wen.bean.JsonResult;
import com.swagger.wen.dao.CustomerRepository;
import com.swagger.wen.model.Customer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Think on 2018/6/16.
 */
@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {

    @Autowired
    private CustomerRepository customerRepository;

    /**
     * 获取所有顾客
     * @return
     */
    @ApiOperation(value="获取所有顾客", notes="获取所有顾客")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getAllCustomerList(){
        JsonResult jsonResult = new JsonResult();
        List<Customer> customerList = customerRepository.findAll();
        jsonResult.setStatus("ok");
        jsonResult.setMsg("成功");
        jsonResult.setResult(customerList);
        return ResponseEntity.ok(jsonResult);
    }

    @ApiOperation(value="根据id获取顾客", notes="根据id获取顾客")
    @ApiImplicitParam(name = "pk", value = "顾客id", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/getCustomerByPk/{pk}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getCustomerByPk(@PathVariable(value = "pk") Integer pk){
        JsonResult jsonResult = new JsonResult();
        Customer customer = customerRepository.getOne(pk);
        jsonResult.setStatus("ok");
        jsonResult.setMsg("成功");
        jsonResult.setResult(customer);
        return ResponseEntity.ok(jsonResult);
    }

    @ApiOperation(value="根据id删除顾客", notes="根据id删除顾客")
    @ApiImplicitParam(name = "pk", value = "顾客id", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "/removeCustomerByPk/{pk}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> removeCustomerByPk(@PathVariable(value = "pk") Integer pk){
        JsonResult jsonResult = new JsonResult();
        customerRepository.deleteById(pk);
        jsonResult.setStatus("ok");
        jsonResult.setMsg("成功");
        return ResponseEntity.ok(jsonResult);
    }

    @ApiOperation(value="增加一个顾客", notes="增加一个顾客")
    @ApiImplicitParam(name = "customer", value = "顾客信息", required = true, dataType = "Customer")
    @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> addCustomer(@RequestBody Customer customer){
        JsonResult jsonResult = new JsonResult();
        customer = customerRepository.save(customer);
        jsonResult.setStatus("ok");
        jsonResult.setMsg("成功");
        jsonResult.setResult(customer);
        return ResponseEntity.ok(jsonResult);
    }

    @ApiOperation(value="修改一个顾客", notes="修改一个顾客")
    @ApiImplicitParam(name = "customer", value = "顾客信息", required = true, dataType = "Customer")
    @RequestMapping(value = "/updateCustomer", method = RequestMethod.PUT)
    public ResponseEntity<JsonResult> updateCustomer( @RequestBody Customer customer){
        JsonResult jsonResult = new JsonResult();
        customer = customerRepository.saveAndFlush(customer);
        jsonResult.setStatus("ok");
        jsonResult.setMsg("成功");
        jsonResult.setResult(customer);
        return ResponseEntity.ok(jsonResult);
    }

}
